package com.linkhub.oms.data.sync.api.wms.domain.request;

import com.alibaba.fastjson.annotation.JSONField;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.List;
import java.util.stream.Collectors;

import com.linkhub.oms.data.sync.api.wms.domain.dto.WmsEntryOrderBatchCreateDTO;

import java.util.Arrays;

/**
 * @author Jaycle
 * @version 1.0.0
 * @Description k.java
 * @createTime 2024/06/15 08:56
 */
@NoArgsConstructor
@Data
public class EntryOrderBatchCreateRequest {

    public static EntryOrderBatchCreateRequest createRequest( WmsEntryOrderBatchCreateDTO dto ) {
        EntryOrderBatchCreateRequest request = new EntryOrderBatchCreateRequest();
        EntryOrderBatchCreateRequest.Request innerRequest = new EntryOrderBatchCreateRequest.Request();
        EntryOrderBatchCreateRequest.Orders orders = new EntryOrderBatchCreateRequest.Orders();
        EntryOrderBatchCreateRequest.Order order = new EntryOrderBatchCreateRequest.Order();

        // Map entryOrder
        EntryOrderBatchCreateRequest.EntryOrder entryOrder = new EntryOrderBatchCreateRequest.EntryOrder();
        entryOrder.setOrderType(dto.getOrderType());
        entryOrder.setExpectStartTime(dto.getExpectStartTime());
        entryOrder.setLogisticsName(dto.getLogisticsName());
        entryOrder.setExpressCode(dto.getExpressCode());
        entryOrder.setSupplierCode(dto.getSupplierCode());
        entryOrder.setSupplierName(dto.getSupplierName());
        entryOrder.setOwnerCode(dto.getOwnerCode());
        entryOrder.setWarehouseCode(dto.getWarehouseCode());
        entryOrder.setEntryOrderCode(dto.getEntryOrderCode());
        entryOrder.setUserDefined1(dto.getUserDefined1());

        // Map orderLines
        List<OrderLine> orderLinesList = dto.getProductInfo().stream().map(productParam -> {
            OrderLine orderLine = new OrderLine();
            orderLine.setOwnerCode(dto.getOwnerCode());
            orderLine.setUserDefined1(dto.getUserDefined1());
            orderLine.setItemCode(productParam.getItemCode());
            orderLine.setItemId(productParam.getItemId());
            orderLine.setPlanQty(productParam.getPlanQty());
            return orderLine;
        }).collect(Collectors.toList());
        EntryOrderBatchCreateRequest.OrderLines orderLines = new EntryOrderBatchCreateRequest.OrderLines();
        orderLines.setOrderLineList(orderLinesList);

        // Set the order fields
        order.setEntryOrder(entryOrder);
        order.setOrderLines(orderLines);

        // Set the orders field
        orders.setOrderList(Arrays.asList(order));

        // Set the request fields
        innerRequest.setOrders(orders);
        innerRequest.setOutBizCode(dto.getOutBizCode());
        request.setRequest(innerRequest);

        return request;
    }

    @JSONField(name = "request")
    private Request request;

    @NoArgsConstructor
    @Data
    public static class Request {
        /**
         * outBizCode
         * 外部业务编码，消息ID，用于去重
         */
        @JSONField(name = "outBizCode")
        private String outBizCode;

        @JSONField(name = "orders")
        private Orders orders;
    }

    @NoArgsConstructor
    @Data
    public static class Orders {
        @JSONField(name = "order")
        private List<Order> orderList;
    }

    @NoArgsConstructor
    @Data
    public static class Order {
        @JSONField(name = "entryOrder")
        private EntryOrder entryOrder;

        @JSONField(name = "orderLines")
        private OrderLines orderLines;

        @JSONField(name = "packages")
        private Packages packages;
    }

    @NoArgsConstructor
    @Data
    public static class EntryOrder {
        /**
         * entryOrderCode
         * 入库单号，必填，oms生成唯一ID
         */
        @JSONField(name = "entryOrderCode")
        private String entryOrderCode;

        /**
         * ownerCode
         * 货主编码，必填
         */
        @JSONField(name = "ownerCode")
        private String ownerCode;

        /**
         * warehouseCode
         * 仓库编码，必填
         */
        @JSONField(name = "warehouseCode")
        private String warehouseCode;

        /**
         * orderCreateTime
         * 订单创建时间
         */
        @JSONField(name = "orderCreateTime")
        private String orderCreateTime;

        /**
         * isRelease
         * 释放状态，默认2
         */
        @JSONField(name = "isRelease")
        private String isRelease;

        /**
         * orderType
         * 入库单类型，必填，提货时固定为CGRK，物流退货，固定为：WLTH（物流退货）
         */
        @JSONField(name = "orderType")
        private String orderType;

        /**
         * expectStartTime
         * 预期到货时间
         */
        @JSONField(name = "expectStartTime")
        private String expectStartTime;

        /**
         * logisticsName
         * 物流公司名称
         */
        @JSONField(name = "logisticsName")
        private String logisticsName;

        /**
         * expressCode
         * 运单号
         */
        @JSONField(name = "expressCode")
        private String expressCode;

        /**
         * supplierCode
         * 供应商编码
         */
        @JSONField(name = "supplierCode")
        private String supplierCode;

        /**
         * supplierName
         * 供应商名称
         */
        @JSONField(name = "supplierName")
        private String supplierName;

        /**
         * documentMaker
         * 制单人
         */
        @JSONField(name = "documentMaker")
        private String documentMaker;

        /**
         * remark
         * 备注
         */
        @JSONField(name = "remark")
        private String remark;

        /**
         * userDefined1
         * 自定义1，多货主模式下为虚拟货主，必填
         */
        @JSONField(name = "userDefined1")
        private String userDefined1;

        /**
         * userDefined2
         * 自定义2
         */
        @JSONField(name = "userDefined2")
        private String userDefined2;

        /**
         * userDefined3
         * 自定义3
         */
        @JSONField(name = "userDefined3")
        private String userDefined3;

        /**
         * userDefined4
         * 自定义4
         */
        @JSONField(name = "userDefined4")
        private String userDefined4;
    }

    @NoArgsConstructor
    @Data
    public static class OrderLines {
        @JSONField(name = "orderLine")
        private List<OrderLine> orderLineList;
    }

    @NoArgsConstructor
    @Data
    public static class OrderLine {
        /**
         * ownerCode
         * 必填，与上面ownerCode一致
         */
        @JSONField(name = "ownerCode")
        private String ownerCode;

        /**
         * itemCode
         * 必填，与新建商品的itemCode一致
         */
        @JSONField(name = "itemCode")
        private String itemCode;

        @JSONField(name = "itemId")
        private String itemId;

        @JSONField(name = "itemName")
        private String itemName;

        /**
         * planQty
         * 计划数量
         */
        @JSONField(name = "planQty")
        private String planQty;

        /**
         * virtualWarehouse
         * 虚拟仓库
         */
        @JSONField(name = "virtualWarehouse")
        private String virtualWarehouse;

        /**
         * inventoryType
         * 质量状态，固定为ZP
         */
        @JSONField(name = "inventoryType")
        private String inventoryType = "ZP";

        @JSONField(name = "productDate")
        private String productDate;

        @JSONField(name = "expireDate")
        private String expireDate;

        @JSONField(name = "batchCode")
        private String batchCode;

        @JSONField(name = "stockinDate")
        private String stockinDate;

        /**
         * userDefined1
         * 自定义1，多货主模式下为虚拟货主，必填
         */
        @JSONField(name = "userDefined1")
        private String userDefined1;

        @JSONField(name = "userDefined2")
        private String userDefined2;

        @JSONField(name = "userDefined3")
        private String userDefined3;

        @JSONField(name = "userDefined4")
        private String userDefined4;

        @JSONField(name = "lineNo")
        private String lineNo;

        @JSONField(name = "customJson")
        private String customJson;

        @JSONField(name = "customField1")
        private String customField1;

        @JSONField(name = "customField2")
        private String customField2;

        @JSONField(name = "customField3")
        private String customField3;

        @JSONField(name = "customField4")
        private String customField4;

        @JSONField(name = "customField5")
        private String customField5;

        @JSONField(name = "customField6")
        private String customField6;

        @JSONField(name = "customField7")
        private String customField7;

        @JSONField(name = "customField8")
        private String customField8;
    }

    @NoArgsConstructor
    @Data
    public static class Packages {
        @JSONField(name = "package")
        private List<Package> packageList;
    }

    @NoArgsConstructor
    @Data
    public static class Package {
        @JSONField(name = "packageCode")
        private String packageCode;

        @JSONField(name = "userDefined1")
        private String userDefined1;

        @JSONField(name = "userDefined2")
        private String userDefined2;

        @JSONField(name = "userDefined3")
        private String userDefined3;

        @JSONField(name = "userDefined4")
        private String userDefined4;

        @JSONField(name = "items")
        private Items items;
    }

    @NoArgsConstructor
    @Data
    public static class Items {
        @JSONField(name = "item")
        private List<Item> itemList;
    }

    @NoArgsConstructor
    @Data
    public static class Item {
        @JSONField(name = "lineNo")
        private String lineNo;

        /**
         * itemCode
         * 与新建商品的itemCode一致
         */
        @JSONField(name = "itemCode")
        private String itemCode;

        @JSONField(name = "itemName")
        private String itemName;

        /**
         * planQty
         * 必填,应收货品数量
         */
        @JSONField(name = "planQty")
        private String planQty;

        @JSONField(name = "stockinDate")
        private String stockinDate;

        @JSONField(name = "productDate")
        private String productDate;

        @JSONField(name = "expireDate")
        private String expireDate;

        @JSONField(name = "inventoryType")
        private String inventoryType;

        @JSONField(name = "batchCode")
        private String batchCode;

        @JSONField(name = "virtualWarehouse")
        private String virtualWarehouse;

        /**
         * userDefined1
         * 自定义批次1，多货主模式下为虚拟货主，必填
         */
        @JSONField(name = "userDefined1")
        private String userDefined1;

        @JSONField(name = "userDefined2")
        private String userDefined2;

        @JSONField(name = "userDefined3")
        private String userDefined3;

        @JSONField(name = "userDefined4")
        private String userDefined4;

        @JSONField(name = "lpn")
        private String lpn;
    }
}
